<?php
declare (strict_types = 1);
namespace app\school\model\school;

use app\common\logic\Attach;
use app\common\model\BaseModel;
// use app\school\event\subscribes\account\User as UserSubscribe;
use app\course\model\base\Course;
use app\course\model\coupon\Coupon;
use app\user\logic\user\Account;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

/**
 * 定义机构数据模型
 */
class School extends BaseModel
{
    use softDelete;
    // 表名
    protected $name = 'school';

    // 设置隐藏字段
    protected $hidden = ['create_time', 'update_time', 'delete_time'];

    protected $type = [
        'fullcategorypath'  => 'array',
    ];

    // 定义表主键
    protected $pk = 'id';

    protected static $statusArr = [
        0 => '已禁用',
        1 => '已认证',
        2 => '待审核',
        3 => '认证驳回',
    ];

    // 全局查询范围    -    禁用全局
    // protected $globalScope = ['status'];

    // 设置状态查询条件

    public static function getStatusText()
    {
        return self::$statusArr;
    }

    public function getLegalIDCardAttr($value): string
    {
        return substr_replace($value, '**************', 0, 14);
    }

    public function scopeStatus($query, $status = 1)
    {
        $query->where('status', $status);
    }

    // 设置机构类型查询条件
    public function scopePublic($query)
    {
        $query->where('school_type', 1);
    }

    // 设置推荐查询条件
    public function scopeRecommend($query)
    {
        $query->order('best_sort', 'ASC');
    }

    public function getSchoolNameById($id)
    {
        return $this->where('id', $id)->value('title');
    }

    public function couponCount()
    {
        return $this
            ->hasMany(coupon::class, 'mhm_id')
            ->whereRaw('receive_count < count')
            ->where('online', 1)                         // 线上卡
            ->where('use_etime', '>', time())            // 使用时间、未过期
            ->whereNull('delete_time')                   // 未删除
            // ->whereColumn('count', '>', 'receive_count') // 未领完
            ->where('derive_stime', '<', time())         // 领取开始时间后
            ->where('derive_etime', '>', time())         // 领取结束时间内
            ;
    }

    // 机构等级
    public function levelText()
    {
        return $this->belongsTo(SchoolLevel::class, 'level_id')->bind(['level_text' => 'title']);
    }

    // 机构等级
    public function levelInfo()
    {
        return $this->belongsTo(SchoolLevel::class, 'level_id');
    }

    // logo地址
    public function logoUrl()
    {
        return $this->belongsTo(Attach::class, 'logo')->bind(['logo_url' => 'fileurl']);
    }

    public function LegalIDcardPositiveUrl()
    {
        return $this->belongsTo(Attach::class, 'legal_IDcard_positive')->bind(['legal_IDcard_positive_url' => 'fileurl']);
    }

    public function LegalIDcardSideUrl()
    {
        return $this->belongsTo(Attach::class, 'legal_IDcard_side')->bind(['legal_IDcard_side_url' => 'fileurl']);
    }

    public function businessLicenseUrl()
    {
        return $this->belongsTo(Attach::class, 'business_license')->bind(['business_license_url' => 'fileurl']);
    }

    public function course()
    {
        return $this->hasMany(Course::class, 'mhm_id');
    }

    /**
     * 当前机构申请人信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-11
     *
     * @return BelongsTo [type]                         [description]
     */
    public function apply()
    {
        return $this->belongsTo(Account::class, 'user_id');
    }
}
